package coder;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.handler.codec.http.websocketx.WebSocket08FrameEncoder;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class NumberConvertByte {

    private static final int BYTE_BIT_LENGTH = 8;
    private static final int INT_BYTE_LENGTH = 4;
    private static final int SHORT_BYTE_LENGTH = 2;
    private static final int BYTE_BYTE_LENGTH = 1;


    public static void main(String[] args) {

        ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer();
        for (int i = 1; i < 20; i++) {
            byte[] b= NumberConvertByte.IntToByteArray(i);
            for (Byte aByte : b) {
                buffer.writeByte(aByte);
            }
            System.out.println(Arrays.toString(b));
            System.out.println(buffer.readInt());
//            byte[] bytes = new byte[buffer.readableBytes()];
//            buffer.readBytes(bytes);
//            System.out.println(NumberConvertByte.ByteArrayToInt(bytes));

//            buffer.clear();
        }

    }




    public static byte[] IntToByteArray(int n) {
        byte[] b = new byte[4];
        b[0] = (byte) (n & 0xff);
        b[1] = (byte) (n >> 8 & 0xff);
        b[2] = (byte) (n >> 16 & 0xff);
        b[3] = (byte) (n >> 24 & 0xff);
        return b;
    }
    //将低字节在前转为int，高字节在后的byte数组(与IntToByteArray1想对应)
    public static int ByteArrayToInt(byte[] bArr) {
        if(bArr.length!=4){
            return -1;
        }
        return (int) ((((bArr[3] & 0xff) << 24)
                | ((bArr[2] & 0xff) << 16)
                | ((bArr[1] & 0xff) << 8)
                | ((bArr[0] & 0xff) << 0)));
    }



    public static Byte[] toObjects(byte[] bytesPrim) {
        Byte[] bytes = new Byte[bytesPrim.length];

        int i = 0;
        for (byte b : bytesPrim) bytes[i++] = b; // Autoboxing

        return bytes;
    }

}
